<template>
  <div>
    <van-nav-bar
      title="地图找房"
      left-arrow
      @click-left="$router.back()"
      fixed
    />
    <div class="mapA" v-if="flag <= 1">
      <baidu-map
        class="bm-view"
        ak="Ez4CadNKRfZGVC1C1IcMymYsWspdpPLW"
        :center="center"
        :zoom="zoom"
      >
        <bm-navigation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-navigation>
        <bm-label v-for="(item, index) in list" :key="index" :content="item.label + '<br>' + item.count" :position="{ lng: item.coord.longitude, lat: item.coord.latitude }" @click="clickMap({id: item.value, coord: { lng: item.coord.longitude, lat: item.coord.latitude }})"/>
      </baidu-map>
    </div>
    <div class="mapB" v-if="flag >= 2">
      <baidu-map
        class="bm-view"
        ak="Ez4CadNKRfZGVC1C1IcMymYsWspdpPLW"
        :center="center"
        :zoom="zoom"
      >
        <bm-navigation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-navigation>
        <bm-label v-for="(item, index) in list" :key="index" :content="item.label + '&nbsp;&nbsp;&nbsp;' + item.count" :position="{ lng: item.coord.longitude, lat: item.coord.latitude }" @click="clickHouse(item.value)"/>
      </baidu-map>
    </div>
    <van-popup v-model="show" :overlay-style="{backgroundColor:'rgb(0 0 0 / 0%)'}" position="bottom">
      <div class="houseList">
        <div class="banner">
          <div class="title">
            <h4>房屋列表</h4>
          </div>
          <div class="botton">
            <div @click="$router.push('/search')">更多房源</div>
          </div>
        </div>
        <div class="info">
          <HouseList v-for="(item, index) in houselist" :key="index" :item="item" />
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
import HouseList from '@/components/HouseList.vue'
import { getMap, getHouse } from '@/api/homo'
import { getCity } from '@/utils/storage'

import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
import BmLabel from 'vue-baidu-map/components/overlays/Label.vue'
import BmNavigation from 'vue-baidu-map/components/controls/Navigation.vue'
export default {
  components: {
    BaiduMap,
    BmLabel,
    BmNavigation,
    HouseList
  },
  data () {
    return {
      center: { lng: 0, lat: 0 },
      zoom: 0,
      list: [],
      flag: 0,
      show: false,
      houselist: []
    }
  },
  created () {
    getMap(getCity().value).then(res => {
      this.list = res.body
    })
    this.center = getCity().label
  },
  methods: {
    loadMap (obj) {
      getMap(obj.id).then(res => {
        this.list = res.body
      })
      this.center = obj.coord
      if (this.flag >= 2) {
        this.zoom = 15
      } else {
        this.zoom = 12
      }
    },
    clickMap (obj) {
      console.log(obj)
      this.list = []
      this.center = { lng: 0, lat: 0 }
      this.flag++
      this.loadMap(obj)
    },
    async clickHouse (id) {
      const res = await getHouse({ cityId: id })
      this.houselist = res.body.list
      this.show = true
    }
  }
}
</script>

<style lang="less" scoped>
.houseList {
  .banner {
    height: 64px;
    display: flex;
    background: #c0c0c2;
    .title{
      flex: 1;
      padding: 0 0 0 15px;
    }
    .botton{
      width: 70px;
      line-height: 64px;
      font-size: 14px;
    }
  }
  .info {
    height: 40vh;
    overflow: scroll;
  }
}
.mapA{
  ::v-deep .BMapLabel {
    line-height: 1.5em !important;
    font-size: 13px !important;
    background: @blue !important;
    border: 3px solid #5cdca7 !important;
    color: #fff;
    padding: 30px 0 0 0!important;
    width: 100px;
    height: 100px;
    border-radius: 50px;
    text-align: center;
  }
}
.mapB{
  ::v-deep .BMapLabel {
    background: @blue !important;
    border: none !important;
    color: #fff;
    border-radius: 5px;
  }
}
.bm-view {
  width: 100%;
  height: 100vh;
}
</style>
